package com.SunSoft.PhoneRemoteControl;

import android.media.AudioRecord;
import android.media.AudioRecord.OnRecordPositionUpdateListener;
import android.util.Log;
import java.io.IOException;
import java.io.RandomAccessFile;

public class g
{
  private static final int[] a = { 44100, 22050, 11025, 8000 };
  private AudioRecord b = null;
  private int c = 0;
  private String d = null;
  private i e;
  private RandomAccessFile f;
  private short g;
  private int h;
  private short i;
  private int j;
  private int k;
  private int l;
  private int m;
  private byte[] n;
  private int o;
  private AudioRecord.OnRecordPositionUpdateListener p = new h(???);

  private g()
  {
    try
    {
      this.i = 16;
      this.g = 1;
      this.k = 1;
      this.h = 44100;
      this.l = 2;
      this.m = 5292;
      this.j = (this.m * 2 * this.i * this.g / 8);
      if (this.j < AudioRecord.getMinBufferSize(44100, 2, 2))
      {
        this.j = AudioRecord.getMinBufferSize(44100, 2, 2);
        this.m = (this.j / this.i * 2 * this.g / 8);
        Log.w(g.class.getName(), "Increasing buffer size to " + Integer.toString(this.j));
      }
      this.b = new AudioRecord(this.k, this.h, 2, this.l, this.j);
      if (this.b.getState() == 1)
        break label245;
      label245: throw new Exception("AudioRecord initialization failed");
    }
    catch (Exception localException)
    {
      if (localException.getMessage() != null)
      {
        Log.e(g.class.getName(), localException.getMessage());
        this.e = i.d;
        return;
        this.b.setRecordPositionUpdateListener(this.p);
        this.b.setPositionNotificationPeriod(this.m);
        this.c = 0;
        this.e = i.a;
        return;
      }
      Log.e(g.class.getName(), "Unknown error occured while initializing recording");
    }
  }

  public static g a()
  {
    return new g();
  }

  public final void a(String paramString)
  {
    try
    {
      if (this.e == i.a)
        this.d = paramString;
      return;
    }
    catch (Exception paramString)
    {
      if (paramString.getMessage() != null)
      {
        Log.e(g.class.getName(), paramString.getMessage());
        this.e = i.d;
        return;
      }
      Log.e(g.class.getName(), "Unknown error occured while setting output path");
    }
  }

  public final void b()
  {
    int i1;
    int i2;
    boolean bool = true;
    try
    {
      if (this.e != i.a)
        break label397;
      if (this.b.getState() != 1)
        break label434;
      i1 = 1;
      if (this.d == null)
        break label439;
      i2 = 1;
      if ((i2 & i1) != 0)
      {
        this.f = new RandomAccessFile(this.d, "rw");
        this.f.setLength(0L);
        this.f.writeBytes("RIFF");
        this.f.writeInt(0);
        this.f.writeBytes("WAVE");
        this.f.writeBytes("fmt ");
        this.f.writeInt(Integer.reverseBytes(16));
        this.f.writeShort(Short.reverseBytes(1));
        this.f.writeShort(Short.reverseBytes(this.g));
        this.f.writeInt(Integer.reverseBytes(this.h));
        this.f.writeInt(Integer.reverseBytes(this.h * this.i * this.g / 8));
        this.f.writeShort(Short.reverseBytes((short)(this.g * this.i / 8)));
        this.f.writeShort(Short.reverseBytes(this.i));
        this.f.writeBytes("data");
        this.f.writeInt(0);
        this.n = new byte[this.m * this.i / 8 * this.g];
        this.e = i.b;
        return;
      }
      Log.e(g.class.getName(), "prepare() method called on uninitialized recorder:" + String.valueOf(this.b.getState()));
      String str = g.class.getName();
      StringBuilder localStringBuilder = new StringBuilder("prepare() method failed:");
      if (this.d == null)
        break label392;
      Log.e(str, String.valueOf(bool) + "," + this.d);
      this.e = i.d;
      label392: label397: label434: label439: return;
    }
    catch (Exception localException)
    {
      while (true)
      {
        if (localException.getMessage() != null)
          Log.e(g.class.getName(), localException.getMessage());
        while (true)
        {
          while (true)
          {
            this.e = i.d;
            return;
            bool = false;
          }
          Log.e(g.class.getName(), "prepare() method called on illegal state");
          c();
          this.e = i.d;
          return;
          Log.e(g.class.getName(), "Unknown error occured in prepare()");
        }
        i1 = 0;
      }
      i2 = 0;
    }
  }

  public final void c()
  {
    if (this.e == i.c)
      f();
    while (true)
    {
      this.e = i.a;
      return;
      if (this.e != i.b)
        continue;
      try
      {
        this.f.close();
      }
      catch (IOException localIOException)
      {
        Log.e(g.class.getName(), "I/O exception occured while closing output file");
      }
    }
  }

  public final void d()
  {
    if (this.b == null)
      return;
    this.b.release();
    this.b = null;
  }

  public final void e()
  {
    if (this.e == i.b)
    {
      this.o = 0;
      this.b.startRecording();
      this.b.read(this.n, 0, this.n.length);
      this.e = i.c;
      return;
    }
    Log.e(g.class.getName(), "start() called on illegal state");
    this.e = i.d;
  }

  public final void f()
  {
    if (this.e == i.c)
      this.b.stop();
    try
    {
      this.f.seek(4L);
      this.f.writeInt(Integer.reverseBytes(this.o + 36));
      this.f.seek(40L);
      this.f.writeInt(Integer.reverseBytes(this.o));
      this.f.close();
      this.e = i.e;
      return;
    }
    catch (IOException localIOException)
    {
      while (true)
      {
        Log.e(g.class.getName(), "I/O exception occured while closing output file");
        this.e = i.d;
      }
      Log.e(g.class.getName(), "stop() called on illegal state");
      this.e = i.d;
    }
  }
}